import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';

class Page1 extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<Page1> {
  // CalendarController _calendarController = CalendarController();
  TextEditingController _moodController = TextEditingController();

  // 存储每天的心情
  Map<DateTime, String> _moods = {};

  // 当前选定的日期
   DateTime _selectedDate = DateTime.now();

  @override
  void dispose() {
    // _calendarController.dispose();
    _moodController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text('日历和心情'),
      // ),
      body: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/img.png'), // 请替换为您的背景图像路径
              fit: BoxFit.cover,
            )),
        child: Padding(
          padding:  EdgeInsets.only(top: 44),
          child: Column(
            children: [
              TableCalendar(
                firstDay: DateTime.utc(2010, 10, 16),
                lastDay: DateTime.utc(2030, 3, 14),
                focusedDay: DateTime.now(),
                onDaySelected: (date, events) {
                  setState(() {
                    _selectedDate = date;
                  });
                  _showMoodDialog(date);
                },
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextField(
                  controller: _moodController,
                  decoration: InputDecoration(
                    labelText: 'Fill in the mood',
                  ),
                ),
              ),
              SizedBox(height: 20),
              _selectedDate != null
                  ? Text(
                '$_selectedDate of mood:',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                ),
              )
                  : Container(),
              _selectedDate != null
                  ? Text(
                _moods[_selectedDate] ?? 'No mood record',
              )
                  : Container(),
            ],
          ),
        ),
      ),
    );
  }

  // 显示填写心情的对话框
  void _showMoodDialog(DateTime date) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Fill in the mood'),
          content: TextField(
            controller: _moodController,
            decoration: InputDecoration(
              labelText: 'Fill in your mood today here...',
            ),
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('cancel'),
            ),
            TextButton(
              onPressed: () {
                setState(() {
                  _moods[date] = _moodController.text;
                  _moodController.clear();
                });
                Navigator.of(context).pop();
              },
              child: Text('save'),
            ),
          ],
        );
      },
    );
  }
}

